package com.zzk.lanouhn.myapplication1.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.KindsDetailsContentsAdapter;
import com.zzk.lanouhn.myapplication1.adapters.SubclassAdapter;
import com.zzk.lanouhn.myapplication1.intefaces.SubclassItemCliclListener;
import com.zzk.lanouhn.myapplication1.modles.KindsDetailsContent;
import com.zzk.lanouhn.myapplication1.modles.Subclass;
import com.zzk.lanouhn.myapplication1.utils.HttpUtils;
import com.zzk.lanouhn.myapplication1.view.MyGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * 分类页面
 * Created by mafei on 16/9/6.
 */
public class KindsDetailsActivity extends AppCompatActivity implements SubclassItemCliclListener {
    private RecyclerView recy;
    private MyGridView contentGrid;
    private KindsDetailsContentsAdapter kindsDetailsContentAdapter;
    private List<KindsDetailsContent.DataBean> kindsDetailsContent = new ArrayList<KindsDetailsContent.DataBean>();
    private List<KindsDetailsContent.DataBean> temp = new ArrayList<KindsDetailsContent.DataBean>();
    private Subclass sub;
    private SubclassAdapter adapter;
    private String id;
    private TextView tv_all, tv_selected;
    private ImageView iv_back, iv_change;
    private String type_id = "1";
    private String selectid = "";
    private int page = 0;

    private List<String> checkedIds = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kindsitemdetails);
        initView();
        initData();
        initEvent();
        getSubDataFromWeb();
        getContentDataFromWeb();
        tv_selected.setTextColor(Color.BLACK);
    }

    private void initView() {
        recy = (RecyclerView) findViewById(R.id.rv_subclass_kinds_details);
        tv_all = (TextView) findViewById(R.id.tv_all_kinds_details);
        tv_selected = (TextView) findViewById(R.id.tv_selected_kinds_details);
        contentGrid = (MyGridView) findViewById(R.id.gv_content_kinds_details);
        iv_back = (ImageView) findViewById(R.id.iv_back_kinds_details);
        //iv_change = (ImageView) findViewById(R.id.iv_changeview_kinds_details);
    }

    private void initData() {
        Bundle bundle = this.getIntent().getExtras();
        id = bundle.getString("id");
    }

    private void initEvent() {
        tv_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type_id = "0";
                tv_all.setTextColor(Color.BLACK);
                tv_selected.setTextColor(Color.GRAY);
                getContentDataFromWeb();
            }
        });
        tv_selected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type_id = "1";
                tv_selected.setTextColor(Color.BLACK);
                tv_all.setTextColor(Color.GRAY);
                getContentDataFromWeb();
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        iv_change.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });

//        contentGrid.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<GridView>() {
//
//            @Override
//            public void onRefresh(PullToRefreshBase<GridView> refreshView) {
//                page+=1;
//                selectContentData();
//                getContentDataFromWeb();
//            }
//        });
        contentGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(KindsDetailsActivity.this, ShowKindsDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("goodsid", kindsDetailsContent.get(position).getId());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }

    private void getSubDataFromWeb() {
        String subUrl = "http://open4.bantangapp.com/category/categoryInfo?";
        String subDataUrl = "app_id=com.jzyd.BanTang&client_id=bt_app_android" +
                "&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d" +
                "&track_user_id=&oauth_token=&track_deviceid=863121028526879" +
                "&track_device_info=MI+2S&channel_name=xiaomi&app_installtime=1473128006" +
                "&app_versions=5.8.7" +
                "&os_versions=5.0.2&screensize=720&v=19&id=" + id;
        try {
            HttpUtils.doPostAsyn(subUrl, subDataUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    sub = gson.fromJson(result, Subclass.class);
                    if (sub.getData().getSubclass_list().size() > 0) {
                        handler.sendEmptyMessage(1);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getContentDataFromWeb() {
        String s = String.valueOf(page);
        String contentUrl = "http://open4.bantangapp.com/community/post/listByCategory?";
        String contentDataUrl = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=&oauth_token=&track_deviceid=863121028526879&track_device_info=MI+2S&channel_name=xiaomi&app_installtime=1473128006" +
                "&app_versions=5.8.7&os_versions=5.0.2" +
                "&screensize=720&v=19&page=0&pagesize=10" +
                "&id=" + id + "&type_id=" + type_id;
        try {
            HttpUtils.doPostAsyn(contentUrl, contentDataUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    kindsDetailsContent = gson.fromJson(result, KindsDetailsContent.class).getData();
                    if (kindsDetailsContent.size() > 0) {
                        handler.sendEmptyMessage(2);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void selectContentData() {
        String s = String.valueOf(page);
        String url = "http://open4.bantangapp.com/community/post/listBySubclass?";
        String dataUrl = "app_id=com.jzyd.BanTang" +
                "&client_id=bt_app_android" +
                "&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d" +
                "&track_user_id=&oauth_token=&track_deviceid=863121028526879" +
                "&track_device_info=MI+2S&channel_name=xiaomi" +
                "&app_installtime=1473128006&app_versions=5.8.7" +
                "&os_versions=5.0.2&screensize=720&v=19&page=0&pagesize=10" +
                "&ids=" + selectid + "&type_id=" + type_id;
        try {
            HttpUtils.doPostAsyn(url, dataUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    kindsDetailsContent = gson.fromJson(result, KindsDetailsContent.class).getData();
                    if (kindsDetailsContent.size() > 0) {
                        handler.sendEmptyMessage(2);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    adapter = new SubclassAdapter(KindsDetailsActivity.this, sub);
                    adapter.SubclassAdapter(KindsDetailsActivity.this);
                    recy.setAdapter(adapter);
                    LinearLayoutManager manager = new LinearLayoutManager(KindsDetailsActivity.this);
                    manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    recy.setLayoutManager(manager);
                    break;
                case 2:
                    kindsDetailsContentAdapter = new KindsDetailsContentsAdapter(KindsDetailsActivity.this, kindsDetailsContent);
                    contentGrid.setAdapter(kindsDetailsContentAdapter);
                    break;
                default:

                    break;
            }
        }
    };

    @Override
    public void onItemClick(String ids, boolean isChecked) {

        //判断checkbox是否为选中状态
        if (isChecked) {
            checkedIds.add(ids);//将选中过的checkbox的id加入list中

            if (selectid.isEmpty())
                selectid = ids;//此时只选择一项
            else {
                selectid = selectid + "%2C" + ids;//选择两项以上
            }

        } else {//取消checkbox
            checkedIds.remove(ids);//首先将要取消的checkbox的id从list中移除

            selectid = "";//初始化id
            if (checkedIds.size() > 0) {//取消某几项后id的拼接
                for (int i = 0; i < checkedIds.size(); i++) {

                    if (i == 0)//表示只留下了一项
                        selectid = checkedIds.get(i);
                    else//留下两项以上
                        selectid += "%2C" + checkedIds.get(i);
                }
            } else {//全部取消后显示的内容
                getContentDataFromWeb();
                return;
            }
        }

        selectContentData();

    }
}
